package com.example.star.model;


import android.util.Log;

import com.example.star.api_service.ApiUser;
import com.example.star.contract.UserContract;
import com.example.star.entity.User;
import com.example.star.retrofit.ApiClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class UserModel implements UserContract.Model {
    private static final String TAG = "UserModel";

    @Override
    public void login(User user, final OnLoginListener listener) {
        ApiUser apiUser = ApiClient.getApiUser();
        Call<User> call = apiUser.login(user.getUsername(), user.getPassword());

        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.isSuccessful()) {
                    User user = response.body();
                    if (user != null && user.isSuccess()) {
                        listener.onSuccess();
                    } else {
                        String errorMessage = user != null ? user.getMessage() : "登录失败，未知错误";
                        listener.onFailure(errorMessage);
                    }
                } else {
                    try {
                        String errorBody = response.errorBody().string();
                        Log.e(TAG, "登录请求失败，错误码：" + response.code() + "，错误信息：" + errorBody);
                        listener.onFailure("请求失败，错误码：" + response.code() + "，错误信息：" + errorBody);
                    } catch (Exception e) {
                        Log.e(TAG, "解析错误响应失败：" + e.getMessage());
                        listener.onFailure("请求失败，错误码：" + response.code());
                    }
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Log.e(TAG, "登录网络请求失败：" + t.getMessage());
                listener.onFailure("网络错误: " + t.getMessage());
            }
        });
    }

    @Override
    public void register(User user, final OnRegisterListener listener) {
        ApiUser apiUser = ApiClient.getApiUser();
        Call<User> call = apiUser.register(user.getUsername(),user.getNickname(), user.getPassword());

        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.isSuccessful()) {
                    User user = response.body();
                    if (user != null && user.isSuccess()) {
                        listener.onSuccess();
                    } else {
                        String errorMessage = user != null ? user.getMessage() : "注册失败，未知错误";
                        listener.onFailure(errorMessage);
                    }
                } else {
                    try {
                        String errorBody = response.errorBody().string();
                        Log.e(TAG, "注册请求失败，错误码：" + response.code() + "，错误信息：" + errorBody);
                        listener.onFailure("请求失败，错误码：" + response.code() + "，错误信息：" + errorBody);
                    } catch (Exception e) {
                        Log.e(TAG, "解析错误响应失败：" + e.getMessage());
                        listener.onFailure("请求失败，错误码：" + response.code());
                    }
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Log.e(TAG, "注册网络请求失败：" + t.getMessage());
                listener.onFailure("网络错误: " + t.getMessage());
            }
        });
    }
}
